﻿@using VinhSon.Intranet.Models
@model VinhSon.Intranet.Models.UserGroupModel
@{
    ViewBag.Title = "Update Role";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section script
{
    <script type="text/javascript">
        function submitForm() {
            if (window.App.FormPanel1.getForm().isValid())
                request('@Url.Action("Edit")', window.App.FormPanel1.getValue(), function(result) {
                    showNotification(window.parent, result, function() {
                        window.parent.App.gvData.getStore().reload();
                        window.parent.App.winEdit.close();
                    });
                }, function() {
                    window.Ext.Msg.show(
                        {
                            title: "Confirm",
                            buttons: window.Ext.Msg.YES,
                            fn: function() {
                                window.parent.App.winEdit.close();
                            },
                            icon: window.Ext.Msg.ERROR,
                            msg: "There some errors occour when update. Please try again later."
                        });
                });
        }

        function addPermission() {
            window.App.winAdd.show();
            window.App.winAdd.load();
        }

        function reloadPermissionGrid() {
            window.App.gvPermission.getStore().reload();
        }

        function handlerCommand(command, record) {
            if (command == 'Delete')
                window.Ext.Msg.show({
                    title: "Confirm",
                    buttons: window.Ext.Msg.YESNO,
                    fn: function(btn) {
                        if (btn == 'yes')
                            request(
                                '@Url.Action("RemovePermission")',
                                { id: record.data.Id, rid: '@Model.Id' },
                                function(result) {
                                    showNotification(window.parent, result);
                                    window.App.gvPermission.getStore().reload();
                                },
                                function() {
                                    window.Ext.Msg.show(
                                        {
                                            title: "Confirm",
                                            buttons: window.Ext.Msg.YES,
                                            icon: window.Ext.Msg.ERROR,
                                            msg: "There some errors occour when update. Please try again later."
                                        });
                                });
                    },
                    icon: window.Ext.Msg.QUESTION,
                    msg: "Do you really want to delete this record?"
                });
            else if (command == 'DeleteUser')
                window.Ext.Msg.show({
                    title: "Confirm",
                    buttons: window.Ext.Msg.YESNO,
                    fn: function(btn) {
                        if (btn == 'yes')
                            request(
                                '@Url.Action("RemoveUserGroupUser")',
                                { id: record.data.Id, rid: '@Model.Id' },
                                function(result) {
                                    showNotification(window.parent, result);
                                    window.App.gvUser.getStore().reload();
                                },
                                function() {
                                    window.Ext.Msg.show(
                                        {
                                            title: "Confirm",
                                            buttons: window.Ext.Msg.YES,
                                            icon: window.Ext.Msg.ERROR,
                                            msg: "There some errors occour when update. Please try again later."
                                        });
                                });
                    },
                    icon: window.Ext.Msg.QUESTION,
                    msg: "Do you really want to delete this record?"
                });
        }

        function addUser() {
            window.App.winUser.show();
            window.App.winUser.load();
        }

        function reloadUserGrid() {
            window.App.gvUser.getStore().reload();
        }
    </script>
}

@(Html.X()
      .Viewport()
      .Layout(LayoutType.Border)
      .Items(panel =>
                 {
                     panel.Add(Html.X()
                                   .FormPanel()
                                   .ID("FormPanel1")
                                   .Region(Region.North)
                                   .Method(HttpMethod.POST)
                                   .BodyPadding(10)
                                   .Border(false)
                                   .Title("Edit Role: " + Model.Name)
                                   .Items(form =>
                                              {
                                                  form.Add(Html.X()
                                                               .HiddenFor(c => c.Id));
                                                  form.Add(Html.X()
                                                               .TextFieldFor(c => c.Name)
                                                               .FieldLabel("Name")
                                                               .AllowBlank(false)
                                                               .EmptyText("Name"));
                                                  form.Add(Html.X()
                                                               .TextAreaFor(c => c.Description)
                                                               .FieldLabel("Description")
                                                               .Width(420)
                                                               .EmptyText("Description"));
                                                  form.Add(Html.X()
                                                               .Button()
                                                               .FormBind(true)
                                                               .Text("Update")
                                                               .MarginSpec("0 5 0 100")
                                                               .OnClientClick("submitForm"));
                                                  form.Add(Html.X()
                                                               .Button()
                                                               .Text("Cancel")
                                                               .OnClientClick("window.parent.App.winEdit.close();"));
                                              }));
                     panel.Add(Html.X()
                                   .TabPanel()
                                   .Layout(LayoutType.Fit)
                                   .Region(Region.Center)
                                   .Items(tab =>
                                              {
                                                  tab.Add(Html.X()
                                                              .Panel()
                                                              .Layout(LayoutType.Border)
                                                              .Title("Permisison")
                                                              .Items(c =>
                                                                         {
                                                                             c.Add(Html.X()
                                                                                       .Toolbar()
                                                                                       .Region(Region.North)
                                                                                       .Items(v =>
                                                                                                  {
                                                                                                      v.Add(Html.X()
                                                                                                                .ToolbarFill());
                                                                                                      v.Add(Html.X()
                                                                                                                .Button()
                                                                                                                .Text("Add Permission")
                                                                                                                .Icon(Icon.Add)
                                                                                                                .OnClientClick("addPermission"));
                                                                                                  }));
                                                                             Panel panel1 = new Panel
                                                                                                {
                                                                                                        Layout = "fit",
                                                                                                        Region = Region.Center,
                                                                                                };
                                                                             c.Add(panel1);
                                                                             panel1.Add(Html.X()
                                                                                            .GridPanel()
                                                                                            .Scroll(ScrollMode.Both)
                                                                                            .ForceFit(true)
                                                                                            .ID("gvPermission")
                                                                                            .Region(Region.Center)
                                                                                            .ColumnModel(v =>
                                                                                                             {
                                                                                                                 v.Add(Html.X()
                                                                                                                           .Column()
                                                                                                                           .Text("Group Name")
                                                                                                                           .DataIndex("GroupName"));
                                                                                                                 v.Add(Html.X()
                                                                                                                           .Column()
                                                                                                                           .Text("Name")
                                                                                                                           .DataIndex("Name"));
                                                                                                                 v.Add(Html.X()
                                                                                                                           .Column()
                                                                                                                           .Text("Description")
                                                                                                                           .DataIndex("Description"));
                                                                                                                 v.Add(Html.X()
                                                                                                                           .Column()
                                                                                                                           .Width(20)
                                                                                                                           .PaddingSpec("0 10")
                                                                                                                           .Align(Alignment.Center)
                                                                                                                           .Commands(b => b.Add(new ImageCommand
                                                                                                                                                    {
                                                                                                                                                            Icon = Icon.Delete,
                                                                                                                                                            CommandName = "Delete",
                                                                                                                                                    }))
                                                                                                                           .Listeners(n =>
                                                                                                                                          {
                                                                                                                                              n.Command.Handler = "handlerCommand(command, record);";
                                                                                                                                          }));
                                                                                                             })
                                                                                            .Store(Html.X()
                                                                                                       .Store()
                                                                                                       .RemoteSort(true)
                                                                                                       .RemotePaging(true)
                                                                                                       .RemoteGroup(true)
                                                                                                       .AutoLoad(true)
                                                                                                       .AutoSync(false)
                                                                                                       .PageSize(50)
                                                                                                       .Model(Html.X()
                                                                                                                  .Model()
                                                                                                                  .Fields("Id",
                                                                                                                          "Name",
                                                                                                                          "GroupName",
                                                                                                                          "Description"))
                                                                                                       .Proxy(Html.X()
                                                                                                                  .AjaxProxy()
                                                                                                                  .Json(true)
                                                                                                                  .Url(Url.Action("GetUserGroupPermissionList",
                                                                                                                                  new
                                                                                                                                      {
                                                                                                                                              id = Model.Id
                                                                                                                                      }))
                                                                                                                  .ActionMethods(v => v.Read = HttpMethod.POST)
                                                                                                                  .Reader(Html.X()
                                                                                                                              .JsonReader()
                                                                                                                              .Root("data")
                                                                                                                              .TotalProperty("total")))
                                                                                                       .AutoLoadParams(v =>
                                                                                                                           {
                                                                                                                               v.Add(new Parameter("start",
                                                                                                                                                   "0",
                                                                                                                                                   ParameterMode.Raw));
                                                                                                                               v.Add(new Parameter("limit",
                                                                                                                                                   "50",
                                                                                                                                                   ParameterMode.Raw));
                                                                                                                           }))
                                                                                            .Features(v => v.Add(Html.X()
                                                                                                                     .Grouping()
                                                                                                                     .GroupHeaderTplString("{name} ({[values.rows.length]} {[values.rows.length > 1 ? \"items\" : \"item\"]})")))
                                                                                            .BottomBar(Html.X()
                                                                                                           .PagingToolbar()));
                                                                         }));
                                                  tab.Add(Html.X()
                                                              .Panel()
                                                              .Layout(LayoutType.Border)
                                                              .Title("User")
                                                              .Items(c =>
                                                                         {
                                                                             c.Add(Html.X()
                                                                                       .Toolbar()
                                                                                       .Region(Region.North)
                                                                                       .Items(v =>
                                                                                                  {
                                                                                                      v.Add(Html.X()
                                                                                                                .ToolbarFill());
                                                                                                      v.Add(Html.X()
                                                                                                                .Button()
                                                                                                                .Text("Add User")
                                                                                                                .Icon(Icon.Add)
                                                                                                                .OnClientClick("addUser"));
                                                                                                  }));
                                                                             Panel panel1 = new Panel
                                                                                                {
                                                                                                        Layout = "fit",
                                                                                                        Region = Region.Center,
                                                                                                };
                                                                             c.Add(panel1);
                                                                             panel1.Add(Html.X()
                                                                                            .GridPanel()
                                                                                            .Scroll(ScrollMode.Both)
                                                                                            .ForceFit(true)
                                                                                            .ID("gvUser")
                                                                                            .Region(Region.Center)
                                                                                            .ColumnModel(v =>
                                                                                                             {
                                                                                                                 v.Add(Html.X()
                                                                                                                           .Column()
                                                                                                                           .Text("Username")
                                                                                                                           .DataIndex("Username"));
                                                                                                                 v.Add(Html.X()
                                                                                                                           .DateColumn()
                                                                                                                           .Text("Created Date")
                                                                                                                           .DataIndex("CreatedDate")
                                                                                                                           .Format(Constant.DATE_TIME_FORMAT));
                                                                                                                 v.Add(Html.X()
                                                                                                                           .DateColumn()
                                                                                                                           .Text("Last Login Date")
                                                                                                                           .DataIndex("LastLoginDate")
                                                                                                                           .Format(Constant.DATE_TIME_FORMAT));
                                                                                                                 v.Add(Html.X()
                                                                                                                           .CheckColumn()
                                                                                                                           .Text("Is Activated")
                                                                                                                           .DataIndex("IsActivated"));
                                                                                                                 v.Add(Html.X()
                                                                                                                           .CheckColumn()
                                                                                                                           .Text("Is LockedOut")
                                                                                                                           .DataIndex("IsLockedOut"));
                                                                                                                 v.Add(Html.X()
                                                                                                                           .Column()
                                                                                                                           .Text("Remark")
                                                                                                                           .DataIndex("Remark"));
                                                                                                                 v.Add(Html.X()
                                                                                                                           .Column()
                                                                                                                           .Width(30)
                                                                                                                           .Align(Alignment.Center)
                                                                                                                           .Commands(b => b.Add(new ImageCommand
                                                                                                                                                    {
                                                                                                                                                            Icon = Icon.Delete,
                                                                                                                                                            CommandName = "DeleteUser",
                                                                                                                                                    }))
                                                                                                                           .Listeners(n =>
                                                                                                                                          {
                                                                                                                                              n.Command.Handler = "handlerCommand(command, record);";
                                                                                                                                          }));
                                                                                                             })
                                                                                            .Store(Html.X()
                                                                                                       .Store()
                                                                                                       .RemoteSort(true)
                                                                                                       .RemotePaging(true)
                                                                                                       .RemoteGroup(true)
                                                                                                       .AutoLoad(true)
                                                                                                       .PageSize(50)
                                                                                                       .AutoSync(false)
                                                                                                       .Model(Html.X()
                                                                                                                  .Model()
                                                                                                                  .Fields("Username",
                                                                                                                          "CreatedDate",
                                                                                                                          "LastLoginDate",
                                                                                                                          "Remark",
                                                                                                                          "IsActivated",
                                                                                                                          "IsLockedOut",
                                                                                                                          "Id"))
                                                                                                       .Proxy(Html.X()
                                                                                                                  .AjaxProxy()
                                                                                                                  .Json(true)
                                                                                                                  .Url(Url.Action("GetUserGroupUserList",
                                                                                                                                  new
                                                                                                                                      {
                                                                                                                                              id = Model.Id
                                                                                                                                      }))
                                                                                                                  .ActionMethods(v => v.Read = HttpMethod.POST)
                                                                                                                  .Reader(Html.X()
                                                                                                                              .JsonReader()
                                                                                                                              .Root("data")
                                                                                                                              .TotalProperty("total")))
                                                                                                       .AutoLoadParams(v =>
                                                                                                                           {
                                                                                                                               v.Add(new Parameter("start",
                                                                                                                                                   "0",
                                                                                                                                                   ParameterMode.Raw));
                                                                                                                               v.Add(new Parameter("limit",
                                                                                                                                                   "50",
                                                                                                                                                   ParameterMode.Raw));
                                                                                                                           }))
                                                                                            .Features(v => v.Add(Html.X()
                                                                                                                     .Grouping()
                                                                                                                     .GroupHeaderTplString("{name} ({[values.rows.length]} {[values.rows.length > 1 ? \"items\" : \"item\"]})")))
                                                                                            .BottomBar(Html.X()
                                                                                                           .PagingToolbar()));
                                                                         }));
                                              }));
                 }))
@(Html.X()
      .Window()
      .ID("winAdd")
      .Resizable(true)
      .Modal(true)
      .Header(true)
      .Width(700)
      .Height(450)
      .Hidden(true)
      .Title("Permission List")
      .Loader(new ComponentLoader
                  {
                          Url = Url.Action("GetPermissions",
                                           new
                                               {
                                                       Model.Id
                                               }),
                          AutoLoad = false,
                          Mode = LoadMode.Frame
                  })
      .Listeners(c =>
                     {
                         c.Close.Handler = "reloadPermissionGrid();";
                     }))
@(Html.X()
      .Window()
      .ID("winUser")
      .Resizable(true)
      .Modal(true)
      .Header(true)
      .Width(700)
      .Height(450)
      .Hidden(true)
      .Title("User List")
      .Loader(new ComponentLoader
                  {
                          Url = Url.Action("GetUsers",
                                           new
                                               {
                                                       Model.Id
                                               }),
                          AutoLoad = false,
                          Mode = LoadMode.Frame
                  })
      .Listeners(c =>
                     {
                         c.Close.Handler = "reloadUserGrid();";
                     }))